#include "UContainerWindow.h"

#include <algorithm>

using namespace std;

TContainerWindow::TContainerWindow(TRectangle Rect, TContainerWindow* parent, TWindowThemer *wt, string name)
:TDockable(Rect, parent, wt, name)
{
ActiveChildindex=0;
}


bool TContainerWindow::Dock(TDockable* WindowToDock, int X, int Y)
{
	WindowToDock->Parent->RemoveChild(WindowToDock);
	AddChild(WindowToDock);
	SetPos(TRectangle(Left, Top, Right, Bottom));
	setActiveChild(Children.size()-1);
	Name = Name + ", "+ WindowToDock->Name;
	return true;
}

TDockable* TContainerWindow::FindWindowByTag(int Tag)
{
	TDockable* result = NULL;

	for (unsigned int i=0; i < Children.size(); ++i)
	{
		result = Children[i]->FindWindowByTag(Tag);
		if (!(result == NULL))
			return result;
	}
	return NULL;
}

TDockable* TContainerWindow::ActiveChild()
{
		if (ActiveChildindex>=0) //FIXME: This fails if there are no windows
		return Children[ActiveChildindex];

	return NULL;
}

void TContainerWindow::KeyUp(SDL_KeyboardEvent* Key) {
	if (ActiveChild()!=NULL) 
		ActiveChild()->KeyUp(Key);
}

void TContainerWindow::KeyDown(SDL_KeyboardEvent* Key)
{
	if (ActiveChild()!=NULL)
		ActiveChild()->KeyDown(Key);

}

void TContainerWindow::MouseDown(int X, int Y, uint8 button)
{
	TDockable::MouseDown(X, Y, button);
}

void TContainerWindow::MouseUp(int X, int Y, uint8 button)
{
	TDockable::MouseUp(X, Y, button);
}

void TContainerWindow::EventHandle(SDL_Event event)
{
	switch (event.type)
	{
		case(SDL_MOUSEMOTION):
		{
			MouseMove(event.motion.x, event.motion.y, event.motion.state);
			break;
		}
		case(SDL_MOUSEBUTTONDOWN):
		{
			MouseDown(event.motion.x, event.motion.y, event.button.button);
			break;
		}
		case(SDL_MOUSEBUTTONUP):
		{
			MouseUp(event.motion.x, event.motion.y, event.button.button);
			break;
		}
		case(SDL_KEYDOWN):
		{
			KeyDown(&event.key);
		}
	}
}

void TContainerWindow::MouseMove(int X, int Y, int mstate)
{
	TDockable::MouseMove(X, Y, mstate);

	if (!ActiveChild()->Contains(X, Y))
		ActiveChild()->MouseLeave();
}

void TContainerWindow::MouseLeave()
{
	TDockable::MouseLeave();
	if (ActiveChild()!=NULL)
		ActiveChild()->MouseLeave();
}

void TContainerWindow::AddChild(TDockable* Child)
{
	Children.push_back(Child);
	Child->Parent = this;
	ActiveChildindex = Children.size()-1;
}

void TContainerWindow::RemoveChild(TDockable* Child)
{
	std::vector<TDockable*>::iterator iter;

	iter = find(Children.begin(), Children.end(), Child);

	if (iter != Children.end()) // needed?
		Children.erase(iter);

	ActiveChildindex = Children.size()-1;

	Name = "";
	if (Children.size() >=1 )
		Name = Children[0]->Name;

	for (unsigned int i=1; i < Children.size(); ++i)
	{
		Name = Name +", "+ Children[i]->Name;
	}

}

void TContainerWindow::setActiveChild(int Child)
{
	ActiveChildindex = Child;
}

void TContainerWindow::SetPos(TRectangle Rect)
{
	TDockable::SetPos(Rect);

	for (unsigned int i=0; i < Children.size(); ++i)
	{
		(Children[i]->SetPos(TRectangle(Left, Top, Right, Bottom)));
	}
}





